package testWater.controller;

import com.perye.dokit.aop.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import testWater.entity.TestWater;
import testWater.service.TestWaterService;
import testWater.query.TestWaterQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Api(tags = "testWaterController管理")
@RestController
@RequestMapping("/api/testWater")
public class TestWaterController {

    @Autowired
    private final TestWaterService service;

    public TestWaterController(TestWaterService service) {
        this.service = service;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@dokit.check('testWater:list')")
    public void download(HttpServletResponse response, TestWaterQueryCriteria criteria) throws IOException {
        service.download(service.queryAll(criteria), response);
    }

    @Log("查询testCpntroller")
    @ApiOperation("查询testCpntroller")
    @GetMapping()
    @PreAuthorize("@dokit.check('testWater:list')")
    public ResponseEntity<Object> getTestWaters(TestWaterQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(service.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增testCpntroller")
    @ApiOperation("新增testCpntroller")
    @PostMapping
    @PreAuthorize("@dokit.check('testWater:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TestWater resources){
        return new ResponseEntity<>(service.create(resources),HttpStatus.CREATED);
    }

    @Log("修改testCpntroller")
    @ApiOperation("修改testCpntroller")
    @PutMapping
    @PreAuthorize("@dokit.check('testWater:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TestWater resources){
        service.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除testCpntroller")
    @ApiOperation("删除testCpntroller")
    @PreAuthorize("@dokit.check('testWater:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        service.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
